_NOWPAGE = 1;
_ISOVER = false;
$(function () {
    // 获取列表信息
    var cid = Common.fn.getUrlParam('cid');
    // cid存在
    if (cid) {
        var result = Model.goods.getClassGoods({
            cid: cid,
            index: 1,
            size: 20
        });
        if (!sessionStorage.getItem('hmdj_stopError')) {
            sessionStorage.setItem('hmdj_stopError', true);
        } else {
            if (result.code) {
                Common.fn.formatOutput(result);
                return false;
            }
        }
        for (var i = 0; i < result.data.length; i++) {
            var item = '<a class="goods-flow-item" href="goods_detail.html?gid=' + result.data[i].goodsId + '"><img class="goods-flow-item-thumb" src="' + result.data[i].picture + '"><img class="goods-flow-item-portrait" src="' + result.data[i].photo + '"><div class="goods-flow-item-price">￥<span class="item-price-title">' + result.data[i].topPrice + '</span></div></a>';
            $('.goods-flow-list').append(item);
        }
        if (result.data.length == 0) {
            weui.alert('该分类下目录为空');
        }
    }
});

// 监听滚轴
window.onscroll = function () {
    var contentHeight = parseInt($('.goods-flow-list').css('height')),
        scrollTop = $(window).scrollTop();
    if (contentHeight - 714 - 300 <= scrollTop && !_ISOVER) {
        loadingMore();
    }
}

// 点击加载更多
function loadingMore() {
    var loading = weui.loading('loading', {
        className: 'custom-classname'
    });
    var cid = Common.fn.getUrlParam('cid');
    // cid存在
    if (cid) {
        _NOWPAGE++;
        var result = Model.goods.getClassGoods({
            cid: cid,
            index: _NOWPAGE,
            size: 20
        });
        if (!sessionStorage.getItem('hmdj_stopError')) {
            sessionStorage.setItem('hmdj_stopError', true);
        } else {
            if (result.code) {
                Common.fn.formatOutput(result);
                return false;
            }
        }
        if(result.data.length == 0) {
            _ISOVER = true;
            $('.load-more-btn').text('没有更多了');
            loading.hide();
            return false;
        }
        for (var i = 0; i < result.data.length; i++) {
            var item = '<a class="goods-flow-item" href="goods_detail.html?gid=' + result.data[i].goodsId + '"><img class="goods-flow-item-thumb" src="' + result.data[i].picture + '"><img class="goods-flow-item-portrait" src="' + result.data[i].photo + '"><div class="goods-flow-item-price">￥<span class="item-price-title">' + result.data[i].topPrice + '</span></div></a>';
            $('.goods-flow-list').append(item);
        }
        if (result.data.length == 0) {
            weui.alert('该分类下目录为空');
        }
    }
    loading.hide();
};